package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.item.entity.Role;
import cn.zysuyuan.item.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-17 18:35
 */

@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分配角色
     */
    @PutMapping("/role/assignPerm")
    public R save(@RequestBody Map<String,Object> map) {
        roleService.assignPerms(map);
        return R.ok();
    }

    /**
     * 添加角色
     */
    @PostMapping("/role")
    public R add(@RequestBody Role role){
        roleService.save(role);
        return R.ok();
    }

    /**
     * 更新角色
     */
    @PutMapping("/role")
    public R update(@RequestBody Role role) {
        roleService.update(role);
        return R.ok();
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/role")
    public R delete(@RequestParam("id") String id) {
        roleService.delete(id);
        return R.ok();
    }

    /**
     * 根据id获取角色信息
     */
    @GetMapping("/role/{id}")
    public R findById(@PathVariable("id") String id) {
        Map<String,Object> map = roleService.findById(id);
        return R.ok(map);
    }


    /**
     * 分页查询角色
     */
    @GetMapping("/role")
    public ResponseEntity<PageResult<Role>> findByPage(@RequestParam Map<String,Object> map) {
        PageResult<Role> result = roleService.selectList(map);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询全部角色
     */
    @GetMapping("/allrole")
    public ResponseEntity<List<Role>> findAllRole() {
        List<Role> list = roleService.findAll();
        return ResponseEntity.ok(list);
    }

}
